package com.capgemini.expense.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.google.appengine.api.datastore.Text;

@XmlType
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@Entity
public class ExpenseForm {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private Date date;
	private int statusId;
	private Long employeeId;
	private Text signature;
	private String remarks;
	private boolean notification;
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private List<Expense> expenses;

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public List<Expense> getExpenses() {
		return expenses;
	}

	public void setExpenses(List<Expense> expenses) {
		this.expenses = expenses;
	}

	public int getStatusId() {
		return statusId;
	}

	public void setStatusId(int statusId) {
		this.statusId = statusId;
	}

	public Long getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}

	public String getSignature() {
		return signature.getValue();
	}

	public void setSignature(String signature) {
		this.signature = new Text(signature);
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public boolean isNotification() {
		return notification;
	}

	public void setNotification(boolean notification) {
		this.notification = notification;
	}

	public Errors validate() {
		List<String> errors = new ArrayList<String>();
		if (signature == null) {
			errors.add("signature is not filled in");
		}
		if (date == null) {
			errors.add("date is not filled in");
		}

		if (expenses == null) {
			errors.add("no expenses");
		} else {
			for (Expense expense : expenses) {
				Errors expenseErrors = expense.validate();
				if (expenseErrors != null) {
					errors.addAll(expenseErrors.getErrors());
				}
			}
		}

		if (errors.size() > 0) {
			Errors errorsObject = new Errors();
			errorsObject.setErrors(errors);
			return errorsObject;
		}

		return null;
	}

}
